package com.shicijiaoliu.controller;

import com.shicijiaoliu.pojo.Author;
import com.shicijiaoliu.pojo.User;
import com.shicijiaoliu.pojo.result;
import com.shicijiaoliu.service.Authorservice;
import com.shicijiaoliu.service.Userservice;
import com.shicijiaoliu.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/author")
@Validated
public class authorcontroller {

    @Autowired
    private Authorservice authorservice;

    @Autowired
    private Userservice userservice;

    @PatchMapping("/beauthor/{bi}")
    public result becomeauthor(@PathVariable("bi")String bi){ //用户只需填写诗人简介，名称是用户名，年代是现代

        if(bi == null){
            return result.error("背景不能为空");
        }
        Map<String,Object> claims = ThreadLocalUtils.getThreadLocal();
        User u = userservice.findByName((String) claims.get("username"));
        if(u.getRuler().equals("作者") ) {
            return result.error("已经成为作者");}
        else{
            Author author = new Author();
            author.setBi(bi);
            author.setName((String) claims.get("username"));
            author.setDynasty("现代");
            u.setRuler("作者");
            userservice.update(u);
            authorservice.addauthor(author);
            return result.success();}
    }
}
